package meijuli.com.join51.business.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.Commodity;

import java.util.List;

/**
 * 订单详情适配器
 *
 * @author wenqin 2017-05-02 16:59
 */

public class CommodityAdapter extends RecyclerView.Adapter<CommodityAdapter.Vh> {

    private LayoutInflater mInflater;
    private List<Commodity> data;
    private Context mContext;

    private OnItemClickListener mOnItemClickListener;

    public CommodityAdapter(Context context, List<Commodity> data) {
        this.data = data;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.adapter_commodity, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(Vh holder, int position) {
        Commodity commodity = data.get(position);
        Glide.with(mContext).load(commodity.getMainPicture()).error(R.mipmap.img_default)
                .into(holder.mCommodityPic);
        holder.mCommodityDescription.setText(commodity.getGoodsName());
        holder.mPrice.setText("¥" + commodity.getPrice());
        holder.mCommodityCnt.setText("x" + commodity.getGoodsNumber());
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        ImageView mCommodityPic;
        TextView mCommodityDescription;
        TextView mPrice;
        TextView mCommodityCnt;

        public Vh(View itemView) {
            super(itemView);
            mCommodityPic = itemView.findViewById(R.id.commodity_pic);
            mCommodityDescription = itemView.findViewById(R.id.commodity_description);
            mPrice = itemView.findViewById(R.id.price);
            mCommodityCnt = itemView.findViewById(R.id.commodity_cnt);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
